package edu.guigu.myapplication123;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import edu.guigu.myapplication123.databinding.ActivityMainBinding;
import edu.guigu.servicedemo.IRemoteAidl;
import edu.guigu.servicedemo.MyData;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnBind.setOnClickListener(this);
        binding.btnBindStart.setOnClickListener(this);
        binding.btnBindPause.setOnClickListener(this);
        binding.btnBindStop.setOnClickListener(this);

        binding.btnBindGetdata.setOnClickListener(this);

        binding.btnBindMessenger.setOnClickListener(this);
        binding.btnBindMessengerAction.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == binding.btnBindStart) {
            if (iRemoteAidl != null) {
                try {
                    iRemoteAidl.action(1);
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if (v == binding.btnBindPause) {
            if (iRemoteAidl != null) {
                try {
                    iRemoteAidl.action(2);
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if (v == binding.btnBindStop) {
            if (iRemoteAidl != null) {
                try {
                    iRemoteAidl.action(3);
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if (v == binding.btnBind) {
            Intent intent = new Intent();
            intent.setPackage("edu.guigu.servicedemo");
            intent.setAction("edu.guigu.servicedemo.ACTION_AIDL");
            bindService(intent, conn, BIND_AUTO_CREATE);
        }

        if (v == binding.btnBindGetdata) {
            if (iRemoteAidl != null) {
                try {
                    MyData myData = iRemoteAidl.getMyData();
                    Toast.makeText(this, myData.getId() + " - " + myData.getContent(), Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        if(v == binding.btnBindMessenger){
            Intent intent = new Intent();
            intent.setPackage("edu.guigu.servicedemo");
            intent.setAction("edu.guigu.servicedemo.ACTION_AIDL_MESSENGER");
            bindService(intent, connMessenger, BIND_AUTO_CREATE);
        }
        if(v == binding.btnBindMessengerAction){
            if(messenger != null){
                Message message = Message.obtain();
                message.what = (int) (Math.abs(Math.random()*10))%3;
                try {
                    messenger.send(message);
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private Messenger messenger = null;
    private ServiceConnection connMessenger = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            messenger = new Messenger(service);
            if (messenger != null) {
                Toast.makeText(MainActivity.this, "绑定远程服务成功", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private IRemoteAidl iRemoteAidl;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iRemoteAidl = IRemoteAidl.Stub.asInterface(service);
            if (iRemoteAidl != null) {
                Toast.makeText(MainActivity.this, "绑定远程服务成功", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
}